<template>
	<view class="bgbox" :style="`height:calc(100vh - ${navigationinfo.state_height}px)`">
		<view class="search pa24 pa_bottom10 wi_100 flex align_center"
			:style="`margin-top:${navigationinfo.state_height}px;`">
			<view class="iconfont i-left ma_right20"  @click="goback">
			</view>
			<view class="flex align_center search_box bg_white flex1 pa_lr30">
				<view class="iconfont i-fdj te_black9 ma_right20"></view> 
				<input class="flex1 f_s28"  confirm-type="search" v-model="state.searchcontent"  maxlength="30"
					@confirm="searchPhone" placeholder="请输入关键字搜索" />
			</view>
			<view class="te_cyan pa_left30 f_s30" @click="goback">取消</view>
		</view>

		<view class="pa24">
			<view id="scrollview">
				<scroll-view scroll-y="true" class="wi_100 flex" :style="`height:${state.scrollviewheight}px`">
					<view class="bg_white bo_radius22" v-if="state.info.length">
						<view class="fw_bold f_s34 ma_bottom10 pa_top30 pa_left30">{{state.info.length}}条相关聊天记录</view>
						<view class="chatlist pa_top30 pa_left30 flex" @click="gochat2(item)"
							v-for="(item,index) in state.info">
							<!-- (store.state.myinfo?.avatar ||'https://vpay-static.oss-cn-shenzhen.aliyuncs.com/static/unmanned/images/main/morentouxiang.png')+'?x-oss-process=image/resize,p_30' -->
							<image class="ma_right30"
								:src="(store.state.friendinfo[item.senderUserId]?.avatar||'https://vpay-static.oss-cn-shenzhen.aliyuncs.com/static/unmanned/images/main/morentouxiang.png')+'?x-oss-process=image/resize,p_30'">
							</image>
							<view class="flex1 chatinfo he_100 pa_bottom30 ma_top4">
								<view class="flex justify_between align_center pa_right30">
									<view class="f_s34">
										{{store.state.friendinfo[item.senderUserId]?.remark_name||item.senderUserId}}
									</view>
									<view class="f_s26 te_blackccc">
										{{gettimes(item.sentTime)}}
									</view>
								</view>
								<view class="f_s26 te_black9 ma_top10 po_rel">
									<view class="ellipsis" style="width:200upx">
										{{item.text}}
									</view>
								</view>

							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>


	</view>
</template>

<script setup lang="ts">
	import { showToast, getnavigationinfo, timestampToDate, getscrollviewheight, throttle } from '@/utils/utils'
	import { friend_search } from '@/api/addressbook'
	import { onLoad, onShow ,onReady} from '@dcloudio/uni-app'
	import { ref, reactive ,getCurrentInstance} from 'vue'
	import { useStore } from 'vuex'
	const store = useStore()
	const instance = getCurrentInstance(); // 获取组件实例
	const navigationinfo = ref<any>(uni.getStorageSync('navigationinfo'))
	const state = reactive<any>({
		searchcontent:'',
		search1:{
			conversation:{
				lastMessage:{
					text:''
				}
			}
		},
		scrollviewheight:0,
		content: '',
		nodata: true,
		info: []
	})
	
	onLoad(()=>{
		state.search1 = uni.getStorageSync('search1')
		uni.setStorageSync('search1','')
		searchMessages()
		console.log(state.search1)
		state.searchcontent = state.search1.conversation.lastMessage?.text || ''
	})
	
	onReady(async () => {
		state.scrollviewheight = await getscrollviewheight('#scrollview', instance)
		
		
		
		
	})
	
	
	const gochat2 = (e) => {
	
		// let datas = encodeURIComponent(JSON.stringify(e))
	
		// uni.navigateTo({
		// 	url: `/pages/chat/chat?result=${datas}`
		// })
	
		//targetId
		console.log('gochat2',e)

		uni.navigateTo({
			url: `/pages/chat/chat2?targetId=${e.targetId}&time=${e.receivedTime}&messageUId=${e.messageUId}`
		})
	}
	
	
	const gettimes = (timestamp) => {
	
		const targetDate = new Date(timestamp);
		const now = new Date();
		const yesterday = new Date(now);
		yesterday.setDate(now.getDate() - 1);
	
		// 获取目标时间的年月日时分
		const targetYear = targetDate.getFullYear();
		const targetMonth = targetDate.getMonth() + 1;
		const targetDay = targetDate.getDate();
		const targetHour = targetDate.getHours();
		const targetMinute = targetDate.getMinutes();
	
		// 格式化时分
		const formattedTime = `${targetHour < 10 ? '0' : ''}${targetHour}:${targetMinute < 10 ? '0' : ''}${targetMinute}`;
	
		// 判断是否是今天
		if (
			targetYear === now.getFullYear() &&
			targetMonth === now.getMonth() + 1 &&
			targetDay === now.getDate()
		) {
			return '今天' + formattedTime;
		}
	
		// 判断是否是昨天
		if (
			targetYear === yesterday.getFullYear() &&
			targetMonth === yesterday.getMonth() + 1 &&
			targetDay === yesterday.getDate()
		) {
			return formattedTime;
		}
	
		// 判断是否是一周内
		const oneWeekAgo = new Date(now);
		oneWeekAgo.setDate(now.getDate() - 7);
		if (targetDate > oneWeekAgo) {
			const daysOfWeek = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
			const dayOfWeek = daysOfWeek[targetDate.getDay()];
			return `${dayOfWeek} ${formattedTime}`;
		}
	
		// 都不是，显示年月日
		return `${targetYear}-${targetMonth < 10 ? '0' : ''}${targetMonth}-${targetDay < 10 ? '0' : ''}${targetDay}`;
	
	
	
	}
	
	
	const searchMessages=async ()=>{
		let callback = {
		onSuccess:(res) => {
			state.info = res.t
			console.log('onSuccess',res)
		//...
		},
		onError:(res) => {
			console.log('onError',res)
		//...
		}};
		let code = await store.state.engine.searchMessages(1, state.search1.conversation?.targetId, null, (state.searchcontent || state.search1.conversation.lastMessage?.text), 0, 50, callback);
	}
	
	


	const searchPhone = async () => {
		searchMessages()
	}
	const goback = () => {
		uni.navigateBack({ delta: 1 })
	}
	const gouserinfo = () => {
		// 是否已经是好友
		if (state.info.is_friend) {
			uni.navigateTo({
				url: '/subPackages/addressbook/userinfo/index?type=1&system_code=' + state.info.unique_code
			})
		} else {
			uni.navigateTo({
				url: '/subPackages/addressbook/userinfo/index?unique_code=' + state.info.unique_code
			})
		}
	}
</script>

<style scoped lang="scss">
	.bgbox {
		width: 100vw;
		background: #F1F4F4;

		.search {
			background: #F1F4F4;
			z-index: 9;

			.search_box {
				height: 72upx;
				border-radius: 36upx;
			}
		}

		.head_img {
			width: 76upx;
			height: 76upx;
		}

		.line_bottom {
			border-bottom: 1px solid #F6F6F6;
		}

		.btn {
			width: 100upx;
			height: 52upx;
			line-height: 54upx;
		}

		.pa_top6 {
			padding-top: 6upx;
		}
	}

	.chatlist {
		image {
			width: 94upx;
			height: 94upx;
			border-radius: 30upx;
		}

		.chatinfo {
			border-bottom: 1upx solid #F6F6F6;
		}
	}
</style>